<template>
  <div v-if="proxy.isAuth(['ROOT'])">
    <el-form :inline="true" :model="dataForm" :rules="dataRule" ref="form">
      <el-form-item>
        <el-button
            type="primary"
            :disabled="!proxy.isAuth(['ROOT'])"
            @click="addHandle()"
        >新增部门</el-button>
      </el-form-item>
    </el-form>
    <el-table
        :data="dataForm.deptList"
        :header-cell-style="{ background: '#f5f57fa' }"
        border
        v-loading="data.loading"
    >

      <el-table-column
          type="index"
          header-align="center"
          align="center"
          min-width="60"
          label="序号"
      >
        <template #default="scope">
          <span>{{
              (data.pageIndex - 1) * data.pageSize + scope.$index + 1
            }}</span>
        </template>
      </el-table-column>
      <el-table-column
          prop="deptName"
          header-align="center"
          align="center"
          min-width="80"
          label="部门名称"
      >
      </el-table-column>
      <el-table-column
          prop="tel"
          header-align="center"
          align="center"
          min-width="80"
          label="电话"
      ></el-table-column>
      <el-table-column
          prop="email"
          header-align="center"
          align="center"
          min-width="80"
          label="邮箱"
      ></el-table-column>
      <el-table-column
          prop="deptDesc"
          header-align="center"
          align="center"
          min-width="80"
          label="部门描述"
      >
      </el-table-column>
      <el-table-column
          header-align="center"
          align="center"
          width="180"
          label="操作"
      >
        <template #default="scope">
          <el-button
              type="primary" text class="btn-width"
              v-if="proxy.isAuth(['ROOT'])"
              @click="updateHandle(scope.row)"
          >
            修改
          </el-button>
          <el-button
              type="danger" text class="btn-width"
              v-if="proxy.isAuth(['ROOT'])"
              :disabled="scope.row.root"
              @click="deleteHandle(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <el-dialog
      :title="!dialog.dataForm.id ? '新增' : '修改'"
      v-if="proxy.isAuth(['ROOT'])"
      :close-on-click-modal="false"
      v-model="dialog.visible"
      width="450px"
  >
    <el-form
        :model="dialog.dataForm"
        ref="dialogForm"
        :rules="dialog.dataRule"
        label-width="80px"
    >
      <el-form-item label="部门名" prop="deptName">
        <el-input v-model="dialog.dataForm.deptName" maxlength="20" clearable />
      </el-form-item>
      <el-form-item label="电话" prop="tel">
        <el-input v-model="dialog.dataForm.tel" maxlength="11" show-word-limit clearable />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="dialog.dataForm.email" maxlength="20" clearable />
      </el-form-item>
      <el-form-item label="描述" prop="deptDesc">
        <el-input v-model="dialog.dataForm.deptDesc" maxlength="20" clearable />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-input">
        <el-button @click="dialog.visible = false">取消</el-button>
        <el-button type="primary" @click="dataFormSumbit()">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, getCurrentInstance, onMounted } from "vue";
const { proxy } = getCurrentInstance();
import { dayjs } from "element-plus";

const dataForm = reactive({
  deptName: "",
  deptList: []
});
const dataRule = reactive({

});
const data = reactive({
  dataList: [],
  pageIndex: 1,
  pageSize: 10,
  totalCount: 0,
  loading: false
});
const dialog = reactive({
  visible: false,
  update: false,
  dataForm: {
    id: null,
    tel: null,
    email: null,
    deptName: null,
    deptDesc: null
  },
  dataRule: {
    deptName: [
      {
        required: false,
        pattern: "^[\u4e00-\u9fa5]{1,10}$",
        message: "部门名称格式错误",
      },
    ]
  },
});

function loadDeptList() {
  proxy.$http("/mis/dept/searchAllDept", "GET", null, true, (resp) => {
    dataForm.deptList = resp.list;
  });
}
loadDeptList();


// 添加
function addHandle() {
  dialog.dataForm.id = null;
  dialog.update = false;
  dialog.visible = true;
  // 清空dialogForm控件
  proxy.$nextTick(() => {
    proxy.$refs['dialogForm'].resetFields();
  })
}

// 数据表单提交
function dataFormSumbit() {
  // 验证表单信息是否有效
  proxy.$refs['dialogForm'].validate(valid => {
    // console.log("表单提交中的valid：" + valid);
    if(valid) {
      let json = {
        id: dialog.dataForm.id,
        deptName: dialog.dataForm.deptName,
        deptDesc: dialog.dataForm.deptDesc,
        tel: dialog.dataForm.tel,
        email: dialog.dataForm.email
      };
      proxy.$http(`/mis/dept/${dialog.dataForm.id == null ? 'insert' : 'update'}`, 'POST', json, true, resp => {
        if(resp.code == 200) {
          proxy.$message({
            message: '操作成功',
            type: 'success',
            duration: 1200,
            // 关闭时触发事件
            onClose: () => {
              dialog.visible = false;
              loadDeptList();
            }
          });
        } else {
          // 报错
          proxy.$message({
            message: '操作失败',
            type: 'error',
            duration: 1200
          });
        }
      })
    }
  })
}

function updateHandle(row) {
  dialog.update = true;
  dialog.dataForm = row;
  dialog.visible = true;
}

function deleteHandle(id) {
  if(id == null) {
    proxy.$message({
      message: '没有选中记录',
      type: 'warning',
      duration: 1200
    });
  } else {
    proxy.$confirm('确定要删除选中的记录？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
        .then(() => {
          proxy.$http('/mis/dept/deleteById', 'POST', {id: id}, true, resp => {
            if(resp.code == 200) {
              proxy.$message({
                message: '删除成功',
                type: 'success',
                duration: 1200,
                onClose: () => {
                  loadDeptList();
                }
              });
            } else {
              proxy.$message({
                message: '删除失败',
                type: 'warning',
                duration: 1200
              });
            }
          })
        })
  }
}

</script>

<style scoped lang="less">
@import url(../style.less);
.input {
  width: 110px;
}
.dialog-input {
  width: 100% !important;
}
.btn-width {
  width: 25px;
}
</style>